<?php

namespace app\admin\command;

use app\common\model\Order as OrderModel;
use think\Cache;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Log;

class Order extends Command
{
    protected function configure()
    {
        $this
            ->setName('order')
            ->addOption('count', 'c', Option::VALUE_OPTIONAL, '分会支持统计', '')
            ->setDescription('Store ticket auto change');
    }

    protected function execute(Input $input, Output $output)
    {
        Request()->module('common');
        if ($input->hasOption('count')) {
            $count = model('Order')->alias('o')
                ->join('Store s', 'o.store_id=s.id')
                ->whereNull('s.delete_time')
                ->where('is_cancel', OrderModel::IS_CANCEL_NOT)
                ->where('is_paid', OrderModel::IS_PAID_PAY)
                ->group('store_id')
                ->field('count(o.id) as ticket, sum(o.money) as money, o.store_id')
                ->select();
            foreach($count as $item){
                $ticket_user = model('Order')
                    ->where('is_cancel', OrderModel::IS_CANCEL_NOT)
                    ->where('is_paid', OrderModel::IS_PAID_PAY)
                    ->where('store_id', $item['store_id'])
                    ->group('user_id')
                    ->count();
                model('Store')->where('id', $item['store_id'])->update([
                    'ticket'    => $item['ticket'],
                    'ticket_money'    => $item['money'],
                    'ticket_user'    => $ticket_user,
                ]);
            }

        }

        $output->info("Build Successed!");
    }
}
